package shuishijie.suishijie.util;

import android.text.TextUtils;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by mindx on 2017/4/27.
 */

public class JsonUtil {
    public static String toString(Object o) {
        try {
            return new Gson().toJson(o);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T fromJson(String str, Class<T> c) {
        try {
            return new Gson().fromJson(str, c);
        } catch (Exception e) {
           // Logger.e(str);
            e.printStackTrace();
        }
        return null;
    }

    public static <T> List<T> fromJsonArray(String str, Class<T[]> clazz) {
        ArrayList<T> list = new ArrayList<T>();
        try {
            if (!TextUtils.isEmpty(str)) {
                T[] arr = new Gson().fromJson(str, clazz);
                list.addAll(Arrays.asList(arr));
            }
        } catch (Exception e) {
          //  Logger.e(str);
            e.printStackTrace();
        }
        return list;
    }
}
